﻿using QiBu.Api.Enum;
using QiBu.Api.Helper.Common;
using QiBu.Api.Logic;
using QiBu.Api.Map;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QiBu.Api.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Login(string returnUrl = "")
        {
            ViewBag.RetUrl = returnUrl;
            return View();
        }

        /// <summary>
        /// 认证
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public JsonResult Auth(string userName, string password) 
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                return Json(new { code = (int)EnumJsonStatus.Fail, msg = "参数不能为空！" });
            }
            var user = SYS_UserLogic.Instance.Login(userName, password, IPHelper.GetIPAddress(), true);
            if (user != null && user.UserId > 0)
            {
                FormLoginHelper<SYS_User>.Set(user);
                return Json(new { code = (int)EnumJsonStatus.Success, msg = "认证成功！" });
            }
            else
            {
                return Json(new { code = (int)EnumJsonStatus.Fail, msg = "用户名或密码错误！" });
            }
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        public ActionResult Logout()
        {
            FormLoginHelper<SYS_User>.Logout("/user/login");
            return View();
        }
    }
}
